package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.mapper.AuditFlowMapper;
import com.zygh.policeasset.mapper.SectionMapper;
import com.zygh.policeasset.mapper.TenderMapper;
import com.zygh.policeasset.mapper.UploadFileMapper;
import com.zygh.policeasset.pojo.Project;
import com.zygh.policeasset.pojo.ProjectNameType;
import com.zygh.policeasset.pojo.Section;
import com.zygh.policeasset.pojo.TableType;
import com.zygh.policeasset.pojo.Tender;
import com.zygh.policeasset.pojo.UploadFile;
import com.zygh.policeasset.utils.AuditUtil;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/TenderService.class */
public class TenderService {
    @Autowired
    TenderMapper tenderMapper;
    @Autowired
    UploadFileMapper uploadFileMapper;
    @Autowired
    SectionMapper sectionMapper;
    @Autowired
    AuditFlowMapper auditFlowMapper;
    @Autowired
    AuditFlowService auditFlowService;

    public PageInfo findByProject(String no, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.tenderMapper.findByProject(no));
    }

    public List<ProjectNameType> selectBytype() {
        return this.tenderMapper.selectBytype();
    }

    public List<Tender> findByProjectNoPage(String no) {
        return this.tenderMapper.findByProject(no);
    }

    public PageInfo findAll(String name, Integer status, int pageNum, int pageSize,
                            String time1, String time2,
                            Integer category, Integer userId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Tender> list = this.tenderMapper.findAll(name, status, time1,
                time2, category, userId);
        Section section = new Section();
        for (int i = 0; i < list.size(); i++) {
            section.setTenderId(list.get(i).getId());
            list.get(i).setSectionList(this.sectionMapper.findAll(section));
        }
        return new PageInfo<>(list);
    }

    public Integer findCount() {
        return this.tenderMapper.findCount();
    }

    public PageInfo findByStatus(int status, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.tenderMapper.findByStatus(status));
    }

    public List<Project> getAdd() {
        return this.tenderMapper.getAdd();
    }

    public Integer add(Tender params) {
        return this.tenderMapper.add(params);
    }

    public Integer insert(Tender tender) {
        String now = DateUtil.now();
        String flowNo = AuditUtil.getFlowNo(now);
        tender.setFlowNo(flowNo);
        this.auditFlowService.add(tender.getUserId(), flowNo, now, TableType.TENDER.getName(), TableType.TENDER.getType(), tender.getProjectNo(), null);
        try {
            this.tenderMapper.add(tender);
            List<UploadFile> uploadFileList = tender.getUploadFileList();
            for (int i = 0; i < uploadFileList.size(); i++) {
                uploadFileList.get(i).setProjectId(tender.getId());
                uploadFileList.get(i).setProjectName("tender");
                uploadFileList.get(i).setCreateDate(DateUtil.now());
                this.uploadFileMapper.add(uploadFileList.get(i));
            }
            List<UploadFile> accessoryList = tender.getAccessoryList();
            for (int i2 = 0; i2 < accessoryList.size(); i2++) {
                accessoryList.get(i2).setProjectId(tender.getId());
                accessoryList.get(i2).setProjectName("tender");
                accessoryList.get(i2).setFileType(-1);
                accessoryList.get(i2).setCreateDate(DateUtil.now());
                this.uploadFileMapper.add(accessoryList.get(i2));
            }
            List<Section> sectionList = tender.getSectionList();
            for (int i3 = 0; i3 < sectionList.size(); i3++) {
                sectionList.get(i3).setProjectNo(tender.getProjectNo());
                sectionList.get(i3).setTenderId(tender.getId());
                this.sectionMapper.add(sectionList.get(i3));
                int sectionId = sectionList.get(i3).getId().intValue();
                List<UploadFile> uploadFileList1 = sectionList.get(i3).getUploadFileList();
                for (int j = 0; j < uploadFileList1.size(); j++) {
                    uploadFileList1.get(j).setProjectId(sectionId);
                    uploadFileList1.get(j).setProjectName("section");
                    uploadFileList1.get(j).setCreateDate(DateUtil.now());
                    this.uploadFileMapper.add(uploadFileList1.get(j));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(tender.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer update(Tender tender) {
        int res = 0;
        try {
            res = this.tenderMapper.update(tender);
            this.sectionMapper.remove(Integer.valueOf(tender.getId()));
            List<Section> sectionList = tender.getSectionList();
            for (int i3 = 0; i3 < sectionList.size(); i3++) {
                sectionList.get(i3).setProjectNo(tender.getProjectNo());
                sectionList.get(i3).setTenderId(tender.getId());
                this.sectionMapper.add(sectionList.get(i3));
                int sectionId = sectionList.get(i3).getId().intValue();
                List<UploadFile> uploadFileList1 = sectionList.get(i3).getUploadFileList();
                for (int j = 0; j < uploadFileList1.size(); j++) {
                    uploadFileList1.get(j).setProjectId(sectionId);
                    uploadFileList1.get(j).setProjectName("section");
                    uploadFileList1.get(j).setCreateDate(DateUtil.now());
                    this.uploadFileMapper.add(uploadFileList1.get(j));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(res);
    }

   /* public Integer update(Tender tender) {
        int res = 0;
        try {
            res = this.tenderMapper.update(tender).intValue();
            List<Section> sectionList = tender.getSectionList();
            for (int i = 0; i < sectionList.size(); i++) {
                this.sectionMapper.update(sectionList.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(res);
    }*/

    public Integer updateStatus(int id, int status) {
        return this.tenderMapper.updateStatus(id, status);
    }

    public Integer remove(int id) {
        int res = 0;
        try {
            res = this.tenderMapper.remove(id).intValue();
            this.uploadFileMapper.delete(id, "tender");
            Section section = new Section();
            section.setTenderId(id);
            List<Section> sectionList = this.sectionMapper.findAll(section);
            for (int i = 0; i < sectionList.size(); i++) {
                this.uploadFileMapper.delete(sectionList.get(i).getId().intValue(), "section");
            }
            this.sectionMapper.remove(Integer.valueOf(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(res);
    }

    public List<Tender> toExcel(Integer id, String name, String time1, String time2) {
        return this.tenderMapper.toExcel(id, name, time1, time2);
    }

    public Tender findById(Integer id) {
        Tender tender = this.tenderMapper.findById(id);
        Section section = new Section();
        section.setTenderId(id.intValue());
        if (this.sectionMapper.findAll(section).size() > 0) {
            tender.setSectionList(this.sectionMapper.findAll(section));
        }
        return tender;
    }
}